import React, { Component } from 'react'
import {Route,Switch,Redirect} from 'react-router-dom'
import { IProps  } from './insterfaceFile/index';

class RouterView extends Component<IProps>{
    render() {
        const {routerConfig} = this.props
        // 不包含重定向
        const  routerView = routerConfig.filter((item:any) => !item.redirect)
        // 包含重定向
        const  routerRedirect = routerConfig.filter((item:any) => item.redirect)
        return (
            <Switch>
                    {
                    routerView.map((item:any,index:number)=>{
                        return <Route key={index} path={item.path} render = {
                            props=>{
                                if(item.children){
                                    return <item.component routerConfig={item.children} {...props} />
                                }
                                return <item.component {...props} />
                            }
                        } />
                    })
                }{
                    routerRedirect.map((item:any,index:number)=>{
                        return <Redirect key={index} from={item.path} to={item.redirect} />
                    })
                }
            </Switch>
        )
    }
}
export default RouterView